package cc.idoone.spring_boot_hello.web.servlet;

import cc.idoone.spring_boot_hello.web.filter.HelloFilter;
import cc.idoone.spring_boot_hello.web.listener.HelloListener;
import cc.idoone.spring_boot_hello.web.servlet.HelloServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * <h3>采用自己 SpringBoot 配置bean 的方式进行配置 Servlet, Filter, Listener</h3>
 * <p>
 * 另一种方式：在类定义中使用注解(@WebServlet, @WebFilter, @WebListener)
 * </p>
 *
 * @author hemj
 * @version 2017/9/22
 */
// @Configuration
public class ServletConfiguration {

	/**
	 * 修改 DispatcherServlet 初始配置参数
	 */
	@Bean
	public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
		ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
		// registration.addUrlMappings("*.do");
		// registration.addUrlMappings("*.json");
		return registration;
	}

	@Bean
	public ServletRegistrationBean helloServletRegistration() {
		ServletRegistrationBean registration = new ServletRegistrationBean(new HelloServlet());
		registration.addUrlMappings("/hello/servlet");
		return registration;
	}

	@Bean
	public FilterRegistrationBean helloFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean(new HelloFilter());
		registration.addUrlPatterns("/*");
		return registration;
	}

	@Bean
	public ServletListenerRegistrationBean servletListenerRegistrationBean() {
		ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
		servletListenerRegistrationBean.setListener(new HelloListener());
		return servletListenerRegistrationBean;
	}
}
